<template>
  <div class="login-container">
    <div class="form-box">
      <h1>黑马面经运营后台</h1>
      <el-form :model="ruleForm"
       :rules="rules" 
       class="demo-ruleForm" 
       ref="ruleForm" 
       status-icon>
        <el-form-item label="用户名" prop="username">
          <el-input
            autocomplete="off"
            placeholder="请输入用户名"
            type="password"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            autocomplete="off"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="submitForm('ruleForm')"
           class="submit" 
           type="primary">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default{
    name:'LoginIndex',
    data () {
      var validatePass2 = (rule,value,callback)=>{
        console.log(value,888)
        const reg = /^\d{6,}$/
        if(!reg.test(value)){
          callback(new Error('请输入6位数以上的密码'))
        }else{
          callback()
        }
      }
        return {
            ruleForm:{
              username:'',
              password:''
            },
            rules:{
              username:[
                {required:true,message:'用户名不能为空',trigger:'blur'}
              ],
              password:[
                {required:true,message:'密码不能为空',trigger:'blur'},
                {min:5,max:11,message:'长度在5到11个字符',trigger:['blur','change']}
              ]
            }
        }
    },
    created () {
    },
    computed:{
    },
    methods:{
      submitFrom (formName) {
        /* 兜底校验 */
        this.$refs[formName].validate(async(valid)=>{
          console.log(valid,'valid')
          //valid表示只要有一个表单不满足校验就会返回一个false
          if(valid){
            //全部满足校验  可以发送请求
            //alert('submit!')
            this.$store.dispatch('user/asyncLogin',this.ruleForm)
          }else{
            //最少有一个不满足校验条件
            return false
          }
          
        })
      },
      resetForm(formName){
        this.$refs[formName].resetFields()  //表单充值
      }
    }
}
</script>
<style lang='scss'  scoped>
.login-container {
  background: url(~@/assets/login-bg.svg) no-repeat center / cover;
  width: 100vw;
  height: 100vh;
  // background-color: pink;
  position: relative;
  .form-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 35%;
    background-color: #fff;
    .el-form {
      margin: 60px 40px;
      button {
        font-size: 20px;
        &.submit {
          background-color: #727cf5;
        }

        padding: 18px 30px;
      }
    }
    h1 {
      text-align: center;
      background-color: #727cf5;
      margin: 0;
      padding: 20px 0;
      color: #fff;
      font-weight: 400;
    }
  }
}
</style>